package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.IstandardService;

@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("struts-default")
public class StandardAction implements ModelDriven<Standard>{

	private Standard standard=new Standard();
	
	@Override
	public Standard getModel() {
		
		
		// TODO Auto-generated method stub
		return standard;
	}
	@Autowired
	private IstandardService IstandardService;
	
	@Action(value="standard_save",results={@Result(name="success",location="pages/base/standard.html", type="redirect")})
	public String save(){
		IstandardService.save(standard);
		return "success";
	}
	

	
	
	@Action(value="standard_pageQuery")
	public void pageQuery(){
		List<Standard> list = IstandardService.findAll();
		//Standard[] array = list.toArray(new Standard[list.size()]);
		ServletActionContext.getContext().getValueStack().push(list);
	}
	
	@Action(value="standard_findAll")
	public void standard_findAll(){
		List<Standard> list = IstandardService.findAll();
		
		//存在值栈 前段页面就能够自动
		/*ServletActionContext.getContext().getValueStack().push(list);*/
		String jsonString = JSON.toJSONString(list);
		
		try {
			ServletActionContext.getResponse().getWriter().write(jsonString);
			System.out.println(jsonString+"周永健");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	
	
	
	
	
	
	
}
